import sdk from '../core/index';
import { Counter } from '../core/index';
import UserManager from "../core/manage/UserManager";
import { IBodyCreateConfig } from "../core/rpg/types";
import pet from "../core/rpg/entity/pet";
import { analysisIntent } from "../core/tool/baseScene";
import { mathType } from "../core/tool/face";
import common from "../core/tool/common";
import component, { ComType } from "../core/tool/component";
import { Icard } from "../core/tool/Icard";
import base_component from "./base_component";
import rpg_component from "./rpg_component";
import { FollowService } from "../service/FollowService";

/**
 * 关注组件 - 主要负责UI交互和状态管理
 * 职责：
 * 1. 管理宠物列表状态
 * 2. 处理用户宠物交互
 * 3. 显示宠物信息
 */
export default class follow_component extends component {
    _pets:pet[] = [];

    constructor() {
        super();
    }
    reload(data:any){
        super.reload(data);
        for(let item of data.pet){
            let c = new pet();
            c.setMaster(this.getBody());
            c.load(item);
            let rpg = (c.getComponent(ComType.rpg) as rpg_component)
            rpg.masterId = this.id;
            this._pets.push(c);
        }
    }
    resave():any {
        let saveData = super.resave();
        saveData.pet = []
        for(let pet of this._pets){
            saveData.pet.push(pet.save());
        }
        return saveData;
    }
    /**
     * 给所有宠物添加经验
     * @param amount 经验值
     */
    addExp(amount: number) {
        const result = FollowService.addExperienceToAllPets(this._pets, amount);
        
        if (result.success) {
            // 更新宠物状态
            result.updatedPets.forEach((updatedPet, index) => {
                if (updatedPet.leveledUp) {
                    const base = this._pets[index].getComponent(ComType.base) as base_component;
                    this.client_text(`🔮${base.nickName}升级了！`);
                }
            });
        }
    }
    client_main(){
        let card = sdk.tool.card.create();
        card.title('🔮我的宠物');
        if(this._pets.length == 0){
            card.l('你还没有宠物哦~');
        }else{
            let index = 1;
            for(let pet of this._pets){
                let base = pet.getComponent(ComType.base) as base_component;
                let rpg = pet.getComponent(ComType.rpg) as rpg_component;
                card.l(`[${index}]Lv.${rpg.level} ${base.emoji}${base.nickName}`);
                index++;
            }
        }
        this.registerFollowButtonIntents(card);
        this.client_text(card.getText());
    }
    /**
     * 遣散宠物
     * @param data 意图数据
     */
    client_dismiss(data:analysisIntent){
        const index_data = sdk.tool.common.cover_number(data.Message.content);
        if(!index_data.k1){
            this.client_text('遣散宠物:遣散+ID');
            return;
        }
        
        const petIndex = index_data.k1 - 1;
        const pet = this._pets[petIndex];
        if(!pet){
            this.client_text('随从不存在');
            return;
        }
        
        const petName = pet.getComponent(ComType.base).nickName;
        this._pets.splice(petIndex, 1);
        this.getComponent(ComType.rpg).fighting_end();
        this.client_text(`🔮随从${petName}已遣散`);
    }
    /**
     * 查看宠物
     * @param data 意图数据
     */
    client_look(data:analysisIntent){
        const index_data = sdk.tool.common.cover_number(data.Message.content);
        if(!index_data.k1){
            this.client_text('查看宠物:查看+ID');
            return;
        }
        
        const pet = this._pets[index_data.k1 - 1];
        if(!pet){
            this.client_text('宠物不存在');
            return;
        }
        
        const rpg = pet.getComponent(ComType.rpg) as rpg_component;
        rpg.client_lookAtt(data,'随从档案');
    }
    /**
     * 添加宠物
     * @param config 宠物配置
     */
    client_add(config:IBodyCreateConfig){
        // 检查宠物数量限制
        if (this._pets.length >= 5) {
            this.client_text('🔮宠物数量已达上限 (5)');
            return;
        }
        
        const newPet = new pet();
        newPet.id = Counter.get('petCont');
        newPet.create(config);
        newPet.setMaster(this.getBody());
        
        const rpg = (newPet.getComponent(ComType.rpg) as rpg_component);
        rpg.masterId = this.id;
        
        this._pets.push(newPet);
        this.getComponent(ComType.rpg).fighting_end();
        this.client_text(`🔮随从${newPet.getComponent(ComType.base).nickName}已添加`);
    }
    
    // 注册随从按钮意图的通用方法
    private registerFollowButtonIntents(card: Icard): void {
        const buttonConfigs = [
            { text: '查看', action: 'client_look', mathType: mathType.开头匹配, isHideMath: false },
            { text: '遣散', action: 'client_dismiss', mathType: mathType.开头匹配, isHideMath: false }
        ];
        
        this.registerButtonIntents(card, buttonConfigs);
    }
}

